#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

typedef struct TREENODE TreeNode;
typedef struct NODETYPE NodeType;
typedef struct MINHEAP  MinHeap;
typedef TreeNode* HeapElement;

struct NODETYPE {
	char ch;
	int freq;
};

struct TREENODE{
	NodeType elm;
	TreeNode *left;
	TreeNode *right;
	int high;
};

struct MINHEAP{
	HeapElement *he;
	int capacity;
	int size;
};

void initHeap(MinHeap*);
_Bool HeapEmpty(MinHeap*);
_Bool HeapFull(MinHeap*);
void insertHeap(MinHeap*,HeapElement);
TreeNode *makeHuffmanTree(MinHeap*);
void calcHigh(TreeNode*,int);
int  calcCodeLen(TreeNode *[],int);
void printHeap(MinHeap*);
void printTree1(TreeNode*);
void printTree2(TreeNode*);
TreeNode* makeTree(TreeNode *,char,char *);
_Bool allIsLeaf(TreeNode *[],int);

#define NODE_SIZE 63

int main(void){
	int N;
	char c;
	int codeLen=0;
	scanf("%d",&N);
	NodeType nodes[NODE_SIZE];
	TreeNode *tn[NODE_SIZE];
	for(int i=0;i<N;i++){
		while( ((c=getc(stdin)) == '\n') || (c== ' ') );
		nodes[i].ch=c;
		scanf("%d",&nodes[i].freq);
		tn[i]=malloc(sizeof(TreeNode));
		tn[i]->elm=nodes[i];
		tn[i]->high=0;
		tn[i]->left=tn[i]->right=NULL;
//printf("GET:%c %d\n",nodes[i].ch,nodes[i].freq);
	}
	MinHeap mp;
	MinHeap *h=&mp;
	initHeap(h);
	for(int i=0;i<N;i++){
		insertHeap(h,tn[i]);
	}
//printHeap(h);
	TreeNode *root=makeHuffmanTree(h);
	calcHigh(root,0);
	codeLen=calcCodeLen(tn,N);
//printf("Code Len:%d\n",calcCodeLen(tn,N));
//printf("pre order\n");
//printTree1(root);
//printf("in order\n");
//printTree2(root);
	int students;
	char ans[NODE_SIZE+1];
	TreeNode * tn2[NODE_SIZE];
	scanf("%d",&students);
	for(int i=0;i<students;i++){
		TreeNode *root=malloc(sizeof(TreeNode));
		root->left=root->right=NULL;
		int codeL=0;
		for(int j=0;j<N;j++){
			while( (c=getc(stdin)) != '\n' );
			scanf("%c%s",&c,ans);
			tn2[j]=makeTree(root,c,ans);
//printf("CH:%4cLen :%4ld Freq:%4d\n",nodes[j].ch,strlen(ans),nodes[j].freq);
			codeL=codeL+ strlen(ans)*nodes[j].freq;
		}
//printf("CodeLen is %d\n",codeL);
		if( allIsLeaf(tn2,N) && codeL==codeLen )
			printf("Yes\n");
		else
			printf("No\n");
	}


	return 0;
}

void initHeap(MinHeap *h){
	h->he=malloc(sizeof(HeapElement) * (NODE_SIZE+1));
	h->he[0]=malloc(sizeof(TreeNode));
	(h->he[0]->elm).freq=-1;
	h->capacity=NODE_SIZE;
	h->size=0;
	return;
}
_Bool HeapEmpty(MinHeap *h){
	if( 0 == h->size )
		return true;
	return false;
}
_Bool HeapFull(MinHeap *h){
	if( NODE_SIZE == h->size )
		return true;
	return false;
}
int compareFreq(TreeNode* tn1,TreeNode* tn2){
	if( (tn1->elm).freq < (tn2->elm).freq )
		return -1;
	else if( (tn1->elm).freq == (tn2->elm).freq )
		return 0;
	else
		return 1;
}
void insertHeap(MinHeap *h,HeapElement he){
	int i=++h->size;
	//while( (h->he[i/2]->elm).freq > (he->elm).freq ){
	while( compareFreq(h->he[i/2],he) > 0 ){
		h->he[i]=h->he[i/2];
		i=i/2;
	}
	h->he[i]=he;
	return;
}
HeapElement deleteHeap(MinHeap *h){
	HeapElement ret=h->he[1];
	HeapElement a=h->he[h->size--];
	if( HeapEmpty(h) )
		return ret;
	int father,child;
	for( father=1,child=2*father; child <= h->size ; father=child,child*=2 ){
		if( child != h->size && compareFreq(h->he[child],h->he[child+1]) > 0
			&& compareFreq(h->he[child+1],a) < 0 ){
			child++;
			h->he[father]=h->he[child];
		}else if( compareFreq(h->he[child],a) < 0 ){
			h->he[father]=h->he[child];
		}else{
			break;
		}
	}
	h->he[father]=a;
	return ret;
}
void calcHigh(TreeNode *t,int high){
	if( NULL == t )
		return;
	t->high=high;
	calcHigh(t->left,high+1);
	calcHigh(t->right,high+1);
	return;
}

void printHeap(MinHeap *h){
	for(int i=1;i<=h->size;i++){
		printf("___%4c%5d%5d\n",(h->he[i]->elm).ch,(h->he[i]->elm).freq,h->he[i]->high);
	}
	return;
}
int calcCodeLen(TreeNode *tn[],int len){
	int codeLen=0;
	for(int i=0;i<len;i++){
		codeLen=codeLen+tn[i]->elm.freq*tn[i]->high;
	}
	return codeLen;
}
TreeNode *makeHuffmanTree(MinHeap *h){
	TreeNode *left,*right,*new;
	while( !HeapEmpty(h) ){
		left=deleteHeap(h);
		if( HeapEmpty(h) ){
			return left;
		}
		right=deleteHeap(h);
		new=malloc(sizeof(TreeNode));
		new->left=left;
		new->right=right;
		new->elm.ch='*';
		new->elm.freq=left->elm.freq+right->elm.freq;
		insertHeap(h,new);
	}
	return NULL;
}
void printTree1(TreeNode *t){
	if(NULL==t)
		return;
	printf("__%4c%5d%5d\n",t->elm.ch,t->elm.freq,t->high);
	printTree1(t->left);
	printTree1(t->right);
	return;
}
void printTree2(TreeNode *t){
	if(NULL==t)
		return;
	printTree2(t->left);
	printf("__%4c%5d%5d\n",t->elm.ch,t->elm.freq,t->high);
	printTree2(t->right);
	return;
}

TreeNode* makeTree(TreeNode *root,char ch,char *route){
//	if(NULL==root){
//		root=malloc(sizeof(TreeNode));	
//		root->left=root->right=NULL;
//	}
	int len=strlen(route);
	int i=0;
	do{
		if( '0' == route[i] ){
			if( NULL==root->left ){
				TreeNode *new=malloc(sizeof(TreeNode));
				new->left=new->right=NULL;
				root->left=new;
			}
			root=root->left;
		}else{
			if( NULL==root->right ){
				TreeNode *new=malloc(sizeof(TreeNode));
				new->left=new->right=NULL;
				root->right=new;
			}
			root=root->right;
		}
		i++;
	}while( --len );
	root->elm.ch=ch;
	return root;
}

_Bool allIsLeaf(TreeNode *tn[],int len){
	for(int i=0;i<len;i++){
		if( tn[i]->left || tn[i]->right )
			return false;
	}
	return true;
}
